USE NutritionTracker
GO
IF EXISTS (SELECT 1 FROM sysobjects WHERE ID = object_id(N'AppUser_MeasurementUnit_Update') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
	DROP PROCEDURE AppUser_MeasurementUnit_Update
GO
CREATE PROC AppUser_MeasurementUnit_Update
(
@AppUserID	INT,
@MeasurementUnitID	TINYINT
)
AS
BEGIN
	SET NOCOUNT ON
	
	DECLARE		@MeasurementTypeID	TINYINT

	SELECT		@MeasurementTypeID = MeasurementTypeID
	FROM		MeasurementUnit
	WHERE		MeasurementUnitID = @MeasurementUnitID

	DELETE FROM		AppUser_MeasurementUnit
	WHERE		AppUserID = @AppUserID
	AND		MeasurementTypeID = @MeasurementTypeID

	INSERT INTO		AppUser_MeasurementUnit
					(
					AppUserID,
					MeasurementTypeID,
					MeasurementUnitID
					)
	VALUES		(
				@AppUserID,
				@MeasurementTypeID,
				@MeasurementUnitID
				)

	SET NOCOUNT OFF
END
GO
GRANT EXECUTE ON AppUser_MeasurementUnit_Update TO NUTRKR_WEB
GO